use "dta\DataSet_v28.dta" , clear

************************************************************************
************************* SAMPLE RESTRICTION ***************************
************************************************************************
keep if inlist(party, "sv", "dna", "sp", "v", "krf", "h", "frp", "h_fv")
keep if year>1920
keep if year==first_year

************************************************************************
********************* GENDER IMBALANCE ON ENTERING COHORT***************
************************************************************************
preserve
collapse female, by(year)
scatter female year, mcolor(white) mlcolor(black) connect(l) lcolor(gray) xlabel(1921(3)1936 1945(4)2021, labsize(vsmall) angle(45)) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) ylabel(,angle(0)) xtitle(Election year) ytitle(Fraction women) scheme(s2mono)
graph save figures\gph\GenderImbalanceEnteringCohort.gph, replace
restore

************************************************************************
********************* GENDER IMBALANCE ON LIST POSITIONS ***************
************************************************************************

forvalues i=1(1)2 {
	gen rank_`i'=0
	replace rank_`i'=1 if rank==`i'
}

collapse rank rank_1 rank_2, by(year female)

tw (scatter rank year if female==0, connect(l) lpattern(solid) msymbol(S) mcolor(gray) mlcolor(gray)) ///
(scatter rank year if female==1, connect(l) lpattern(solid) msymbol(O) mcolor(black) mlcolor(black)), ///
ytitle("List position (average)") xtitle("Election year") xscale(range(1921 2017)) xlabel(1921(3)1936 1945(4)2021, angle(45) labsize(vsmall)) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono) ylabel(1(1)10,angle(0)) legend(order(1 "Men" 2 "Women") pos(3) ring(0) col(1) region(lstyle(none)))
graph save figures/gph/Rank.gph, replace

forvalues i=1(1)2 {
	tw (scatter rank_`i' year if female==0, connect(l) lpattern(solid) msymbol(S) mcolor(gray) mlcolor(gray)) ///
	(scatter rank_`i' year if female==1, connect(l) lpattern(solid) msymbol(O) mcolor(black) mlcolor(black)), ///
	ytitle("Position `i' (fraction)") xtitle("Election year") xscale(range(1921 2017)) xlabel(1921(3)1936 1945(4)2021, angle(45) labsize(vsmall)) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono) ylabel(0(0.02)0.1,angle(0)) yline(0.1, lstyle(foreground) lcolor(gs14)) legend(order(1 "Men" 2 "Women") pos(1) ring(0) col(1) region(lstyle(none)))
	graph save figures/gph/Rank`i'.gph, replace
}

cd figures/gph/
graph combine GenderImbalanceEnteringCohort.gph Rank.gph Rank1.gph Rank2.gph, plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) 
graph export ../FigureA7.pdf, replace
cd ../../